package utils;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.util.Map;

// 优化 HttpUtil.java
public class HttpUtil {
    private static final CloseableHttpClient client = HttpClients.createDefault();

    public static String get(String url, Map<String,String> headers) throws Exception {
        HttpGet request = new HttpGet(url);
        headers.forEach(request::setHeader);  // 动态添加请求头

        try (CloseableHttpResponse response = client.execute(request)) {
            return EntityUtils.toString(response.getEntity());
        }
    }

    public static String post(String url, String jsonBody) throws Exception {
        HttpPost request = new HttpPost(url);
        request.setEntity(new StringEntity(jsonBody, ContentType.APPLICATION_JSON));
        return executeWithRetry(request);  // 增加重试机制
    }

    private static String executeWithRetry(HttpUriRequest request) {
        // 实现带指数退避的重试逻辑
        return null;
    }
}